package com.zhongshengnetwork.rightbe.game.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dd.CircularProgressButton;
import com.tencent.connect.common.Constants;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.wang.avi.AVLoadingIndicatorView;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BroadcastDefine;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.activity.MyWebActivity;
import com.zhongshengnetwork.rightbe.common.customview.ActionSheetDialog;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.customview.PublishSelectPicPopupWindow;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.game.model.GameCommonUtils;
import com.zhongshengnetwork.rightbe.game.model.WordModel;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.GameTimeOutModel;
import com.zhongshengnetwork.rightbe.gsonmodel.GuessAnswerTipModel;
import com.zhongshengnetwork.rightbe.gsonmodel.HistorySubjectModel;
import com.zhongshengnetwork.rightbe.gsonmodel.HomeDataModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WordUserinfoModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WordinfoModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.login.LoginActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SubjectWordActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{
    private final static String url = "http://a.app.qq.com/o/simple.jsp?pkgname=com.zhongshengnetwork.rightbe";//Constant.HOST+"downapp.do?";
    private List<String> list;
    private PublishSelectPicPopupWindow menuWindow;

    private TopBarView topbar;

    private TextView subject_word_level;
    private TextView subject_word_title;
    private TextView subject_word_gold;

    private CircularProgressButton subject_word_delete;
    private CircularProgressButton subject_word_tip;
    private CircularProgressButton subject_word_help;
    private CircularProgressButton subject_word_share;

    private CircularProgressButton subject_word_thirdline_1;
    private CircularProgressButton subject_word_thirdline_2;
    private CircularProgressButton subject_word_thirdline_3;
    private CircularProgressButton subject_word_thirdline_4;
    private CircularProgressButton subject_word_thirdline_5;
    private CircularProgressButton subject_word_thirdline_6;
    private CircularProgressButton subject_word_thirdline_7;
    private CircularProgressButton subject_word_thirdline_8;

    private CircularProgressButton subject_word_secondline_1;
    private CircularProgressButton subject_word_secondline_2;
    private CircularProgressButton subject_word_secondline_3;
    private CircularProgressButton subject_word_secondline_4;
    private CircularProgressButton subject_word_secondline_5;
    private CircularProgressButton subject_word_secondline_6;
    private CircularProgressButton subject_word_secondline_7;
    private CircularProgressButton subject_word_secondline_8;

    private CircularProgressButton subject_word_firstline_1;
    private CircularProgressButton subject_word_firstline_2;
    private CircularProgressButton subject_word_firstline_3;
    private CircularProgressButton subject_word_firstline_4;
    private CircularProgressButton subject_word_firstline_5;
    private CircularProgressButton subject_word_firstline_6;
    private CircularProgressButton subject_word_firstline_7;
    private CircularProgressButton subject_word_firstline_8;

    private CircularProgressButton subject_word_answer_first;
    private CircularProgressButton subject_word_answer_second;
    private CircularProgressButton subject_word_answer_third;
    private CircularProgressButton subject_word_answer_fourth;
    private CircularProgressButton subject_word_answer_fifth;
    private CircularProgressButton subject_word_answer_sixth;
    private CircularProgressButton subject_word_answer_seventh;
    private ImageView subject_word_bg;
    private TextView delete_tips;

    private WordinfoModel wordinfoModel = null;
    private WordUserinfoModel wordUserinfoModel = null;
    private List<WordModel> textList;
    private List<String> answerList;
    private boolean isGetTip = false;
    private boolean isChecking = false;

    private SubjectWordActivity.CountDownTime mTime;
    private boolean isTimeOut = false;

    private HomeDataModel homeDataModel;

    private boolean isDeleteMode = false;
    private String deleteIndexs = "";
    private List<String> tempList;
    private AVLoadingIndicatorView avi;

    private LinearLayout subject_word_firstline;
    private LinearLayout subject_word_secondline;
    private LinearLayout subject_word_thirdline;
    private Button answer_right_button;
    private RelativeLayout subject_word_content;

    //历史题目相关的
    private HistorySubjectModel historySubjectModel = null;
    private String subjectIndex;

    private MyIUiListener mIUiListener = new MyIUiListener();
    class MyIUiListener implements IUiListener {
        @Override
        public void onComplete(Object o) {
            Log.e("TAG","分享成功");
            getShareGold();
        }

        @Override
        public void onError(UiError uiError) {
            Log.e("TAG","分享出错");
        }

        @Override
        public void onCancel() {
            Log.e("TAG","分享取消");
        }

        @Override
        public void onWarning(int i) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, mIUiListener);
        if (requestCode == Constants.REQUEST_API) {
            if (resultCode == Constants.REQUEST_QQ_SHARE || resultCode == Constants.REQUEST_QZONE_SHARE || resultCode == Constants.REQUEST_OLD_SHARE) {
                Tencent.handleResultData(data, mIUiListener);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tempList = new ArrayList<>();
        setContentView(R.layout.activity_subject_word);
        homeDataModel = (HomeDataModel)getIntent().getSerializableExtra("model");
        subjectIndex = getIntent().getStringExtra("subjectIndex");
        if (!CommonUtils.isEmpty(subjectIndex)){
            historySubjectModel = (HistorySubjectModel)getIntent().getSerializableExtra("historySubjectModel");
        }
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);

        /*
        topbar.getRightTextView().setText("排行榜");
        topbar.getRightTextView().setTextSize(15);
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken()) || CustomApplication.isAutoLogin == true){
                    startActivity(new Intent(SubjectWordActivity.this, LoginActivity.class));
                    finish();
                    return;
                }
                if (wordUserinfoModel != null){
                    Intent intent = new Intent(SubjectWordActivity.this,GameRangeActivity.class);
                    intent.putExtra("appid",wordUserinfoModel.getAppid());
                    startActivity(intent);
                }

            }
        });
        */
        topbar.getRightTextView().setVisibility(View.INVISIBLE);
        topbar.getRightView().setImageResource(R.drawable.more);
        topbar.getRightView().setVisibility(View.VISIBLE);
        topbar.getRightView().setClickable(true);
        topbar.getRightView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActionSheetDialog actionSheetDialog = new ActionSheetDialog(SubjectWordActivity.this)
                        .builder()
                        .setTitle("请选择")
                        .setCancelable(false)
                        .setCanceledOnTouchOutside(true);
                actionSheetDialog.addSheetItem("排行榜", null, new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        go2GameRange();
                    }
                });
                actionSheetDialog.addSheetItem("历史答题", null, new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        go2History();
                    }
                });
                actionSheetDialog.show();
            }
        });
        if (historySubjectModel != null){
            topbar.getRightView().setVisibility(View.INVISIBLE);
        }

        avi = (AVLoadingIndicatorView)findViewById(R.id.avi);
        list = new ArrayList<String>();
        list.add("分享给微信好友");
        list.add("分享到微信朋友圈");
        list.add("分享给QQ好友");
        list.add("分享到QQ空间");
        //list.add("取消");
        initUI();
        getWordInfo();
        registerBoradcastReceiver();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
        stopTime();
    }

    private void go2GameRange(){
        if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken()) || CustomApplication.isAutoLogin == true){
            startActivity(new Intent(SubjectWordActivity.this, LoginActivity.class));
            finish();
            return;
        }
        if (wordUserinfoModel != null){
            Intent intent = new Intent(SubjectWordActivity.this,GameRangeActivity.class);
            intent.putExtra("appid",wordUserinfoModel.getAppid());
            startActivity(intent);
        }
    }

    private void go2History(){
        if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken()) || CustomApplication.isAutoLogin == true){
            startActivity(new Intent(SubjectWordActivity.this, LoginActivity.class));
            finish();
            return;
        }
        if (wordUserinfoModel != null){
            Intent intent = new Intent(SubjectWordActivity.this,HistorySubjectActivity.class);
            intent.putExtra("appid",wordUserinfoModel.getAppid());
            intent.putExtra("subjectName",homeDataModel.getName());
            startActivity(intent);
        }
    }

    public void onClickWordBg(View view){
        Log.e("TAG","点击了背景\n");
        if (!CommonUtils.isEmpty(homeDataModel.getContenturl()) && homeDataModel.isStatus() == true){
            Intent webIntent = new Intent(SubjectWordActivity.this, MyWebActivity.class);
            webIntent.putExtra(APIKey.urlKey, homeDataModel.getContenturl());
            startActivity(webIntent);
        }
    }

    private void initUI()
    {

        textList = new ArrayList<WordModel>();
        answerList = new ArrayList<String>();

        subject_word_bg = (ImageView) findViewById(R.id.subject_word_bg);
        if (!CommonUtils.isEmpty(homeDataModel.getBgurl())){
            Glide.with(SubjectWordActivity.this).load(homeDataModel.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(subject_word_bg);
        }

        subject_word_level = (TextView) findViewById(R.id.subject_word_level);
        subject_word_title = (TextView) findViewById(R.id.subject_word_title);
        subject_word_gold = (TextView) findViewById(R.id.subject_word_gold);

        delete_tips = (TextView) findViewById(R.id.delete_tips);
        subject_word_delete = (CircularProgressButton) findViewById(R.id.subject_word_delete);
        subject_word_delete.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_delete.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_delete.setText("删除成语");
        subject_word_tip = (CircularProgressButton) findViewById(R.id.subject_word_tip);
        subject_word_tip.setBackgroundColor(getResources().getColor(R.color.limitBackgroundColor));
        subject_word_tip.setStrokeColor(getResources().getColor(R.color.limitBackgroundColor));
        subject_word_tip.setText("提示");
        if (historySubjectModel != null){
            subject_word_tip.setText("解析");
        }
        subject_word_help = (CircularProgressButton) findViewById(R.id.subject_word_help);
        subject_word_help.setBackgroundColor(getResources().getColor(R.color.orangeColor));
        subject_word_help.setStrokeColor(getResources().getColor(R.color.orangeColor));
        subject_word_help.setText("金币");
        subject_word_share = (CircularProgressButton) findViewById(R.id.subject_word_share);
        subject_word_share.setBackgroundColor(getResources().getColor(R.color.lightGreenColor));
        subject_word_share.setStrokeColor(getResources().getColor(R.color.lightGreenColor));
        subject_word_share.setText("分享");

        subject_word_thirdline_1 = (CircularProgressButton) findViewById(R.id.subject_word_thirdline_1);
        subject_word_thirdline_2 = (CircularProgressButton) findViewById(R.id.subject_word_thirdline_2);
        subject_word_thirdline_3 = (CircularProgressButton) findViewById(R.id.subject_word_thirdline_3);
        subject_word_thirdline_4 = (CircularProgressButton) findViewById(R.id.subject_word_thirdline_4);
        subject_word_thirdline_5 = (CircularProgressButton) findViewById(R.id.subject_word_thirdline_5);
        subject_word_thirdline_6 = (CircularProgressButton) findViewById(R.id.subject_word_thirdline_6);
        subject_word_thirdline_7 = (CircularProgressButton) findViewById(R.id.subject_word_thirdline_7);
        subject_word_thirdline_8 = (CircularProgressButton) findViewById(R.id.subject_word_thirdline_8);

        subject_word_secondline_1 = (CircularProgressButton) findViewById(R.id.subject_word_secondline_1);
        subject_word_secondline_2 = (CircularProgressButton) findViewById(R.id.subject_word_secondline_2);
        subject_word_secondline_3 = (CircularProgressButton) findViewById(R.id.subject_word_secondline_3);
        subject_word_secondline_4 = (CircularProgressButton) findViewById(R.id.subject_word_secondline_4);
        subject_word_secondline_5 = (CircularProgressButton) findViewById(R.id.subject_word_secondline_5);
        subject_word_secondline_6 = (CircularProgressButton) findViewById(R.id.subject_word_secondline_6);
        subject_word_secondline_7 = (CircularProgressButton) findViewById(R.id.subject_word_secondline_7);
        subject_word_secondline_8 = (CircularProgressButton) findViewById(R.id.subject_word_secondline_8);

        subject_word_firstline_1 = (CircularProgressButton) findViewById(R.id.subject_word_firstline_1);
        subject_word_firstline_2 = (CircularProgressButton) findViewById(R.id.subject_word_firstline_2);
        subject_word_firstline_3 = (CircularProgressButton) findViewById(R.id.subject_word_firstline_3);
        subject_word_firstline_4 = (CircularProgressButton) findViewById(R.id.subject_word_firstline_4);
        subject_word_firstline_5 = (CircularProgressButton) findViewById(R.id.subject_word_firstline_5);
        subject_word_firstline_6 = (CircularProgressButton) findViewById(R.id.subject_word_firstline_6);
        subject_word_firstline_7 = (CircularProgressButton) findViewById(R.id.subject_word_firstline_7);
        subject_word_firstline_8 = (CircularProgressButton) findViewById(R.id.subject_word_firstline_8);

        subject_word_firstline_1.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_1.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_2.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_2.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_3.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_3.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_4.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_4.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_5.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_5.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_6.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_6.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_7.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_7.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_8.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_firstline_8.setStrokeColor(getResources().getColor(R.color.wordBgColor));

        subject_word_secondline_1.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_1.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_2.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_2.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_3.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_3.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_4.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_4.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_5.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_5.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_6.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_6.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_7.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_7.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_8.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_secondline_8.setStrokeColor(getResources().getColor(R.color.wordBgColor));

        subject_word_thirdline_1.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_1.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_2.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_2.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_3.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_3.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_4.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_4.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_5.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_5.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_6.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_6.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_7.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_7.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_8.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_thirdline_8.setStrokeColor(getResources().getColor(R.color.wordBgColor));


        subject_word_answer_first = (CircularProgressButton) findViewById(R.id.subject_word_answer_first);
        subject_word_answer_second = (CircularProgressButton) findViewById(R.id.subject_word_answer_second);
        subject_word_answer_third = (CircularProgressButton) findViewById(R.id.subject_word_answer_third);
        subject_word_answer_fourth = (CircularProgressButton) findViewById(R.id.subject_word_answer_fourth);
        subject_word_answer_fifth = (CircularProgressButton) findViewById(R.id.subject_word_answer_fifth);
        subject_word_answer_sixth = (CircularProgressButton) findViewById(R.id.subject_word_answer_sixth);
        subject_word_answer_seventh = (CircularProgressButton) findViewById(R.id.subject_word_answer_seventh);

        subject_word_answer_first.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_first.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_second.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_second.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_third.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_third.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_fourth.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_fourth.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_fifth.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_fifth.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_sixth.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_sixth.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_seventh.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        subject_word_answer_seventh.setStrokeColor(getResources().getColor(R.color.wordBgColor));

        subject_word_firstline = (LinearLayout) findViewById(R.id.subject_word_firstline);
        subject_word_secondline = (LinearLayout) findViewById(R.id.subject_word_secondline);
        subject_word_thirdline = (LinearLayout) findViewById(R.id.subject_word_thirdline);
        answer_right_button = (Button) findViewById(R.id.answer_right_button);
        subject_word_content = (RelativeLayout) findViewById(R.id.subject_word_content);
    }

    private void leftPushIn(){
        Animation animation = AnimationUtils.loadAnimation(SubjectWordActivity.this, R.anim.pushfromleft);
        //subject_word_content.startAnimation(animation);

        subject_word_firstline.startAnimation(animation);
        subject_word_secondline.startAnimation(animation);
        subject_word_thirdline.startAnimation(animation);

    }

    private void show(boolean isRight){
        answer_right_button.setAlpha(1.0f);
        Animation animation = AnimationUtils.loadAnimation(SubjectWordActivity.this, R.anim.pushfromleft);
        answer_right_button.startAnimation(animation);
        new Handler().postDelayed(new Runnable(){
            public void run() {
                getWordInfo();
            }
        }, 1000);   //0.5秒

    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        back();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    //菜单点击
    public void onClickMenu(View view){
        switch (view.getId()){
            case R.id.subject_word_tip:
                if (historySubjectModel != null){
                    if (CommonUtils.isEmpty(wordinfoModel.getDetail())){
                        ToastUtil.show(SubjectWordActivity.this,"暂无解析哦");
                    }else {
                        CustomDialog dialog = null;
                        final CustomDialog.Builder builder = new CustomDialog.Builder(SubjectWordActivity.this);
                        builder.setTitle(wordinfoModel.getAnswer());
                        builder.setMessage(wordinfoModel.getDetail());
                        builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        dialog = builder.create();
                        dialog.show();
                    }
                }else {
                    getTip();
                }
                break;
            case R.id.subject_word_help:
                signDo();
                break;
            case R.id.subject_word_share:
                share(view);
                break;
            case R.id.subject_word_delete:
                if (historySubjectModel != null){
                    ToastUtil.show(SubjectWordActivity.this,"你已经回答过该题了");
                    return;
                }
                if (isDeleteMode == true){
                    isDeleteMode = false;
                    deleteIndexs = "";
                    isChecking = false;
                    subject_word_delete.setText("删除成语");
                    delete_tips.setVisibility(View.INVISIBLE);
                }else {
                    isDeleteMode = true;
                    deleteIndexs = "";
                    isChecking = false;
                    subject_word_delete.setText("点击答题");
                    delete_tips.setVisibility(View.VISIBLE);
                }
                deleteMode(isDeleteMode);
                break;
        }
    }

    //点击了文字
    public void onClickText(View view){
        if (historySubjectModel != null){
            return;
        }
        if (isChecking == true){
            return;
        }
        int i = 0;
        switch (view.getId()){
            case R.id.subject_word_firstline_1:
                i = 1;
                break;
            case R.id.subject_word_firstline_2:
                i = 2;
                break;
            case R.id.subject_word_firstline_3:
                i = 3;
                break;
            case R.id.subject_word_firstline_4:
                i = 4;
                break;
            case R.id.subject_word_firstline_5:
                i = 5;
                break;
            case R.id.subject_word_firstline_6:
                i = 6;
                break;
            case R.id.subject_word_firstline_7:
                i = 7;
                break;
            case R.id.subject_word_firstline_8:
                i = 8;
                break;

            case R.id.subject_word_secondline_1:
                i = 9;
                break;
            case R.id.subject_word_secondline_2:
                i = 10;
                break;
            case R.id.subject_word_secondline_3:
                i = 11;
                break;
            case R.id.subject_word_secondline_4:
                i = 12;
                break;
            case R.id.subject_word_secondline_5:
                i = 13;
                break;
            case R.id.subject_word_secondline_6:
                i = 14;
                break;
            case R.id.subject_word_secondline_7:
                i = 15;
                break;
            case R.id.subject_word_secondline_8:
                i = 16;
                break;

            case R.id.subject_word_thirdline_1:
                i = 17;
                break;
            case R.id.subject_word_thirdline_2:
                i = 18;
                break;
            case R.id.subject_word_thirdline_3:
                i = 19;
                break;
            case R.id.subject_word_thirdline_4:
                i = 20;
                break;
            case R.id.subject_word_thirdline_5:
                i = 21;
                break;
            case R.id.subject_word_thirdline_6:
                i = 22;
                break;
            case R.id.subject_word_thirdline_7:
                i = 23;
                break;
            case R.id.subject_word_thirdline_8:
                i = 24;
                break;
        }
        if (textList != null && textList.size() > 0){
            boolean isEmpty = false;
            WordModel model = textList.get(i-1);
            String indexStr = ""+(i-1);
            if (i-1 < 10){
                indexStr = "0"+(i-1);
            }
            if (isDeleteMode == true){
                if (deleteIndexs.contains(indexStr) == false){
                    if (deleteIndexs.length() > 0){
                        deleteIndexs += ",";
                    }
                    deleteIndexs += indexStr;
                    setDeleteIndex(false,(i-1)/8+1,(i-1)%8+1, View.VISIBLE);
                    checkDelete();
                }else {
                    setDeleteIndex(true,(i-1)/8+1,(i-1)%8+1, View.VISIBLE);
                    tempList.clear();
                    List<String> l = CommonUtils.stringToArrayList(deleteIndexs);
                    if (l != null && l.size() > 0){
                        tempList.addAll(l);
                        tempList.remove(indexStr);
                    }
                    if (tempList.size() > 0){
                        deleteIndexs = CommonUtils.listToString(tempList);
                    }else {
                        deleteIndexs = "";
                    }
                }
            }else {
                for (int j = 0; j < answerList.size(); j++){
                    if (answerList.get(j).length() == 0){
                        isEmpty = true;
                        model.setSelect(1);
                        model.setVisible(View.INVISIBLE);
                        setText(model.getText(),(i-1)/8+1,(i-1)%8+1, View.INVISIBLE);
                        answerList.set(j,model.getText());
                        setAnswerText(model.getText(),j);
                        break;
                    }
                }
                textList.set(i-1,model);
                if (isEmpty == true){
                    checkAnswer();
                }
            }

        }
    }

    //点击了答案
    public void onClickAnswer(View view){
        if (historySubjectModel != null){
            return;
        }
        String text = "";
        switch (view.getId()){
            case R.id.subject_word_answer_first:
                if (answerList.size() <= 0){
                    break;
                }
                text = answerList.get(0);
                answerList.set(0,"");
                subject_word_answer_first.setText("");
                break;
            case R.id.subject_word_answer_second:
                /*
                if (answerList.size() <= 1) {
                    break;
                }
                text = answerList.get(1);
                answerList.set(1,"");
                subject_word_answer_second.setText("");
                */
                break;
            case R.id.subject_word_answer_third:
                if (answerList.size() <= 2){
                    break;
                }
                text = answerList.get(2);
                answerList.set(2,"");
                subject_word_answer_third.setText("");
                break;
            case R.id.subject_word_answer_fourth:
                /*
                if (answerList.size() <= 3){
                    break;
                }
                text = answerList.get(3);
                answerList.set(3,"");
                subject_word_answer_fourth.setText("");
                */
                break;
            case R.id.subject_word_answer_fifth:
                if (answerList.size() <= 4){
                    break;
                }
                text = answerList.get(4);
                answerList.set(4,"");
                subject_word_answer_fifth.setText("");
                break;
            case R.id.subject_word_answer_sixth:
                /*
                if (answerList.size() <= 5){
                    break;
                }
                text = answerList.get(5);
                answerList.set(5,"");
                subject_word_answer_sixth.setText("");
                */
                break;
            case R.id.subject_word_answer_seventh:
                if (answerList.size() <= 6){
                    break;
                }
                text = answerList.get(6);
                answerList.set(6,"");
                subject_word_answer_seventh.setText("");
                break;
        }
        if (text.length() > 0 && textList != null && textList.size() > 0){
            for (int i = 0; i < textList.size(); i++){
                WordModel model = textList.get(i);
                if (model.getSelect() == 1 && text.equals(model.getText())){
                    model.setSelect(0);
                    model.setVisible(View.VISIBLE);
                    setText(text,i/8+1,i%8+1, View.VISIBLE);
                    break;
                }
            }
        }
    }

    private void dealWithAnswer(int index){
        if (historySubjectModel != null){
            return;
        }
        String text = "";
        if (index >= answerList.size()){
            return;
        }
        if (index %2 == 1) {
            return;
        }
        switch (index){
            case 0:
                text = answerList.get(0);
                answerList.set(0,"");
                subject_word_answer_first.setText("");
                break;
            case 1:
                text = answerList.get(1);
                answerList.set(1,"");
                subject_word_answer_second.setText("");
                break;
            case 2:
                text = answerList.get(2);
                answerList.set(2,"");
                subject_word_answer_third.setText("");
                break;
            case 3:
                text = answerList.get(3);
                answerList.set(3,"");
                subject_word_answer_fourth.setText("");
                break;
            case 4:
                text = answerList.get(4);
                answerList.set(4,"");
                subject_word_answer_fifth.setText("");
                break;
            case 5:
                text = answerList.get(5);
                answerList.set(5,"");
                subject_word_answer_sixth.setText("");
                break;
            case 6:
                text = answerList.get(6);
                answerList.set(6,"");
                subject_word_answer_seventh.setText("");
                break;
        }
        if (text.length() > 0 && textList != null && textList.size() > 0){
            for (int i = 0; i < textList.size(); i++){
                WordModel model = textList.get(i);
                if (model.getSelect() == 1 && text.equals(model.getText())){
                    model.setSelect(0);
                    model.setVisible(View.VISIBLE);
                    setText(text,i/8+1,i%8+1, View.VISIBLE);
                    break;
                }
            }
        }
    }

    //获取成语信息
    public void getWordInfo(){
        avi.show();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        map.put(APIKey.typeKey, homeDataModel.getType()+"");
        String url = "miniapp/"+homeDataModel.getAppid()+"/getsubjectwordinfo.do";
        if (historySubjectModel != null){
            map.put("id",historySubjectModel.getIndex()+"");
            url = "miniapp/"+homeDataModel.getAppid()+"/getsubjectcontent.do";
        }
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
            @Override
            public void onSuccess(String data) {
                answer_right_button.setAlpha(0.0f);
                avi.hide();
                Log.e("TAG","成语信息："+data+"\n");
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    wordinfoModel = GsonTools.getWordinfoModel(data);
                    wordUserinfoModel = GsonTools.getWordUserinfoModel(data);
                    Log.e("TAG","成语信息："+wordinfoModel.getId()+"\n");
                    if (wordUserinfoModel != null && wordinfoModel != null){
                        //topbar.getTitleView().setText("【"+wordUserinfoModel.getLevelName()+"】");
                        wordUserinfoModel.setPlaylevel(wordUserinfoModel.getPlaylevel());
                        //subject_word_level.setText("第 "+wordinfoModel.getId()+" 题");
                        topbar.getTitleView().setText("第 "+(wordUserinfoModel.getPlaylevel()+1)+" 题");
                        subject_word_gold.setText(""+wordUserinfoModel.getGoldcount());
                        subject_word_title.setText(wordinfoModel.getTitle());
                        String str = wordinfoModel.getAllwords().replace("[","");
                        str = str.replace("]","");
                        List<String> list = CommonUtils.stringToArrayList(str);
                        answerList.clear();
                        for (int i = 0; i < 7; i++){
                            String w = wordinfoModel.getAnswer();
                            if (i < wordinfoModel.getWordcount()){
                                if (historySubjectModel != null){
                                    w = w.substring(i,i+1);
                                }else {
                                    if (i%2==1){
                                        w = w.substring(i,i+1);
                                    }else {
                                        w = "";
                                    }
                                }
                                answerList.add(w);
                            }
                            if (i == 0){
                                if (i < wordinfoModel.getWordcount()){
                                    subject_word_answer_first.setText(w);
                                    subject_word_answer_first.setVisibility(View.VISIBLE);
                                }else {
                                    subject_word_answer_first.setVisibility(View.GONE);
                                }
                            }else if (i == 1){
                                if (i < wordinfoModel.getWordcount()){
                                    subject_word_answer_second.setText(w);
                                    subject_word_answer_second.setVisibility(View.VISIBLE);
                                }else {
                                    subject_word_answer_second.setVisibility(View.GONE);
                                }
                            }else if (i == 2){
                                if (i < wordinfoModel.getWordcount()){
                                    subject_word_answer_third.setText(w);
                                    subject_word_answer_third.setVisibility(View.VISIBLE);
                                }else {
                                    subject_word_answer_third.setVisibility(View.GONE);
                                }
                            }else if (i == 3){
                                if (i < wordinfoModel.getWordcount()){
                                    subject_word_answer_fourth.setText(w);
                                    subject_word_answer_fourth.setVisibility(View.VISIBLE);
                                }else {
                                    subject_word_answer_fourth.setVisibility(View.GONE);
                                }
                            }else if (i == 4){
                                if (i < wordinfoModel.getWordcount()){
                                    subject_word_answer_fifth.setText(w);
                                    subject_word_answer_fifth.setVisibility(View.VISIBLE);
                                }else {
                                    subject_word_answer_fifth.setVisibility(View.GONE);
                                }
                            }else if (i == 5){
                                if (i < wordinfoModel.getWordcount()){
                                    subject_word_answer_sixth.setText(w);
                                    subject_word_answer_sixth.setVisibility(View.VISIBLE);
                                }else {
                                    subject_word_answer_sixth.setVisibility(View.GONE);
                                }
                            }else if (i == 6){
                                if (i < wordinfoModel.getWordcount()){
                                    subject_word_answer_seventh.setText(w);
                                    subject_word_answer_seventh.setVisibility(View.VISIBLE);
                                }else {
                                    subject_word_answer_seventh.setVisibility(View.GONE);
                                }
                            }
                        }
                        textList.clear();
                        if (list != null && list.size() > 0){
                            System.out.print("成语："+list.size());
                            for (int i = 0; i < list.size(); i++){
                                //setText(String text, int line, int index, int visible)
                                setText(list.get(i), (i/8)+1,(i%8)+1,View.VISIBLE);
                                WordModel model = new WordModel();
                                model.setText(list.get(i));
                                model.setSelect(0);
                                model.setVisible(View.VISIBLE);
                                textList.add(model);
                            }
                        }
                    }
                    leftPushIn();
                    if (historySubjectModel != null){
                        topbar.getTitleView().setText(homeDataModel.getName());
                        subject_word_level.setText(subjectIndex);
                    }else {
                        startTime();
                    }
                    //ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                }else {
                    //ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                    CustomApplication.showTip(commonModel,SubjectWordActivity.this);
                }
            }
            @Override
            public void onError(String msg) {
                answer_right_button.setAlpha(0.0f);
                avi.hide();
            }
        });
    }

    //获取提示
    public void getTip(){
        if (wordUserinfoModel == null){
            return;
        }
        if (isGetTip == true){
            return;
        }
        isGetTip = true;
        List<String>list = new ArrayList<String>();
        for (int i = 0; i < answerList.size(); i++){
            if (answerList.get(i).length() == 0){
                list.add(i+"");
            }
        }
        if (list.size() == 0){
            isGetTip = false;
            return;
        }
        //avi.show();
        int x=(int)(Math.random()*list.size());
        final int index = Integer.valueOf(list.get(x));

        String tip = GameCommonUtils.getTip(wordinfoModel.getAnswer(),index,wordUserinfoModel.getPlaylevel(),wordUserinfoModel.getGoldcount());
        if (CommonUtils.isEmpty(tip)){
            isGetTip = false;
            return;
        }else if (tip.length() > 1){
            isGetTip = false;
            ToastUtil.show(SubjectWordActivity.this,tip);
            return;
        }else{
            wordUserinfoModel.setGoldcount(wordUserinfoModel.getGoldcount()-GameCommonUtils.getGoldByLevel(wordUserinfoModel.getPlaylevel()));
            subject_word_gold.setText(""+wordUserinfoModel.getGoldcount());
            answerList.set(index,tip);
            setAnswerText(tip,index);
            for (int ii = 0; ii < textList.size(); ii++){
                WordModel m = textList.get(ii);
                if (m.getSelect() == 0 && m.getText().equals(tip)){
                    setText(tip,ii/8+1,ii%8+1, View.INVISIBLE);
                    textList.set(ii,m);
                    break;
                }
            }
            checkAnswer();
            isGetTip = false;
        }

        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        map.put(APIKey.idKey,wordinfoModel.getId()+"");
        map.put("index",index+"");
        String url = "miniapp/"+homeDataModel.getAppid()+"/getsubjectwordtip.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
            @Override
            public void onSuccess(String data) {
                /*
                avi.hide();
                Log.e("TAG","提示："+data+"\n");
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    //ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                    GuessAnswerTipModel model = GsonTools.getGuessAnswerTipModel(data);
                    wordUserinfoModel.setGoldcount(model.getGold());
                    subject_word_gold.setText(""+wordUserinfoModel.getGoldcount());
                    answerList.set(index,model.getTip());
                    setAnswerText(model.getTip(),index);
                    for (int ii = 0; ii < textList.size(); ii++){
                        WordModel m = textList.get(ii);
                        if (m.getSelect() == 0 && m.getText().equals(model.getTip())){
                            setText(model.getTip(),ii/8+1,ii%8+1, View.INVISIBLE);
                            textList.set(ii,m);
                            break;
                        }
                    }
                    checkAnswer();
                }else {
                    //ToastUtil.show(GuessWordActivity.this,commonModel.getMsg());
                }
                isGetTip = false;
                */
            }
            @Override
            public void onError(String msg) {
                /*
                isGetTip = false;
                avi.hide();
                */
            }
        });
    }

    //检测答案
    public void checkAnswer(){
        Log.e("TAG","检测答案\n");
        int count = 0;
        for (int i = 0; i < answerList.size(); i++){
            if (answerList.get(i).length() > 0){
                count++;
            }
        }
        if (count == answerList.size()){
            if (isChecking == true){
                return;
            }
            isChecking = true;
            avi.show();
            String word = CommonUtils.listToString(answerList);
            word = word.replaceAll(",","");
            word = word.replaceAll(" ","");
            Log.e("TAG","检测答案:"+word);
            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
            map.put(APIKey.idKey,wordinfoModel.getId()+"");
            map.put(APIKey.typeKey,homeDataModel.getType()+"");
            map.put("word",word);
            final String answer = word;
            String url = "miniapp/"+homeDataModel.getAppid()+"/checksubjectwordanswer.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
                @Override
                public void onSuccess(String data) {
                    avi.hide();
                    Log.e("TAG",data);
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        stopTime();
                        isChecking = false;
                        if (CommonUtils.isEmpty(wordinfoModel.getDetail())){
                            //ToastUtil.show(SubjectWordActivity.this,"回答正确");
                            //getWordInfo();
                            show(true);
                        }else {
                            CustomDialog dialog = null;
                            final CustomDialog.Builder builder = new CustomDialog.Builder(SubjectWordActivity.this);
                            builder.setTitle(answer);
                            builder.setMessage(wordinfoModel.getDetail());
                            builder.setPositiveButton("下一题", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    getWordInfo();
                                }
                            });
                            dialog = builder.create();
                            dialog.show();
                        }

                    }else {
                        //ToastUtil.show(SubjectWordActivity.this,commonModel.getMsg());
                        new Handler().postDelayed(new Runnable(){
                            public void run() {
                                for (int i = 0; i < answerList.size(); i++){
                                    dealWithAnswer(i);
                                }
                                isChecking = false;
                                checkTimeOut();
                            }
                        }, 500);   //0.5秒
                    }
                }
                @Override
                public void onError(String msg) {
                    isChecking = false;
                    avi.hide();
                }
            });
        }

    }


    //答案检测
    private void checkDelete(){
        List<String> l = CommonUtils.stringToArrayList(deleteIndexs);
        if (l != null && l.size() >= 4){
            isChecking = true;
            String cy = "";
            for(String index : l){
                WordModel model = textList.get(Integer.valueOf(index));
                cy += model.getText();
            }
            Log.e("成语",cy);
            cy = cy.replaceAll(" ","");
            if (wordinfoModel.getMixword().contains(cy)){
                for (String index : l){
                    int i = Integer.valueOf(index);
                    setDeleteIndex(true,i/8+1,i%8+1, View.INVISIBLE);
                }
            }else {
                for (String index : l){
                    int i = Integer.valueOf(index);
                    setDeleteIndex(true,i/8+1,i%8+1, View.VISIBLE);
                }
            }
            isChecking = false;
            deleteIndexs = "";
        }
    }

    //模式控制
    private void deleteMode(boolean isDelete){
        String bgcolor = "#AA1923";
        if (isDelete == true){
            bgcolor = "#57606F";
        }
        int color = Color.parseColor(bgcolor);
        //第一行
        subject_word_firstline_1.setBackgroundColor(color);
        subject_word_firstline_2.setBackgroundColor(color);
        subject_word_firstline_3.setBackgroundColor(color);
        subject_word_firstline_4.setBackgroundColor(color);
        subject_word_firstline_5.setBackgroundColor(color);
        subject_word_firstline_6.setBackgroundColor(color);
        subject_word_firstline_7.setBackgroundColor(color);
        subject_word_firstline_8.setBackgroundColor(color);

        subject_word_firstline_1.setStrokeColor(color);
        subject_word_firstline_2.setStrokeColor(color);
        subject_word_firstline_3.setStrokeColor(color);
        subject_word_firstline_4.setStrokeColor(color);
        subject_word_firstline_5.setStrokeColor(color);
        subject_word_firstline_6.setStrokeColor(color);
        subject_word_firstline_7.setStrokeColor(color);
        subject_word_firstline_8.setStrokeColor(color);

        //第二行
        subject_word_secondline_1.setBackgroundColor(color);
        subject_word_secondline_2.setBackgroundColor(color);
        subject_word_secondline_3.setBackgroundColor(color);
        subject_word_secondline_4.setBackgroundColor(color);
        subject_word_secondline_5.setBackgroundColor(color);
        subject_word_secondline_6.setBackgroundColor(color);
        subject_word_secondline_7.setBackgroundColor(color);
        subject_word_secondline_8.setBackgroundColor(color);

        subject_word_secondline_1.setStrokeColor(color);
        subject_word_secondline_2.setStrokeColor(color);
        subject_word_secondline_3.setStrokeColor(color);
        subject_word_secondline_4.setStrokeColor(color);
        subject_word_secondline_5.setStrokeColor(color);
        subject_word_secondline_6.setStrokeColor(color);
        subject_word_secondline_7.setStrokeColor(color);
        subject_word_secondline_8.setStrokeColor(color);

        //第三行
        subject_word_thirdline_1.setBackgroundColor(color);
        subject_word_thirdline_2.setBackgroundColor(color);
        subject_word_thirdline_3.setBackgroundColor(color);
        subject_word_thirdline_4.setBackgroundColor(color);
        subject_word_thirdline_5.setBackgroundColor(color);
        subject_word_thirdline_6.setBackgroundColor(color);
        subject_word_thirdline_7.setBackgroundColor(color);
        subject_word_thirdline_8.setBackgroundColor(color);

        subject_word_thirdline_1.setStrokeColor(color);
        subject_word_thirdline_2.setStrokeColor(color);
        subject_word_thirdline_3.setStrokeColor(color);
        subject_word_thirdline_4.setStrokeColor(color);
        subject_word_thirdline_5.setStrokeColor(color);
        subject_word_thirdline_6.setStrokeColor(color);
        subject_word_thirdline_7.setStrokeColor(color);
        subject_word_thirdline_8.setStrokeColor(color);

    }

    //选项控制
    private void setDeleteIndex(boolean isNormal, int line, int index, int visible){
        String bgcolor = "#48CFAD";
        if (isNormal == true){
            bgcolor = "#57606F";
        }
        switch (index){
            case 1:
                if (line == 1){
                    subject_word_firstline_1.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_firstline_1.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_firstline_1.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_1.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_secondline_1.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_secondline_1.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_1.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_1.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_1.setVisibility(visible);
                }
                break;
            case 2:
                if (line == 1){
                    subject_word_firstline_2.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_firstline_2.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_firstline_2.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_2.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_secondline_2.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_secondline_2.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_2.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_2.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_2.setVisibility(visible);
                }
                break;
            case 3:
                if (line == 1){
                    subject_word_firstline_3.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_firstline_3.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_firstline_3.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_3.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_secondline_3.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_secondline_3.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_3.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_3.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_3.setVisibility(visible);
                }
                break;
            case 4:
                if (line == 1){
                    subject_word_firstline_4.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_firstline_4.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_firstline_4.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_4.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_secondline_4.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_secondline_4.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_4.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_4.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_4.setVisibility(visible);
                }
                break;
            case 5:
                if (line == 1){
                    subject_word_firstline_5.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_firstline_5.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_firstline_5.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_5.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_secondline_5.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_secondline_5.setVisibility(visible);
                }else if (line == 3) {
                    subject_word_thirdline_5.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_5.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_5.setVisibility(visible);
                }

                break;
            case 6:
                if (line == 1){
                    subject_word_firstline_6.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_firstline_6.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_firstline_6.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_6.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_secondline_6.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_secondline_6.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_6.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_6.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_6.setVisibility(visible);
                }
                break;
            case 7:
                if (line == 1){
                    subject_word_firstline_7.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_firstline_7.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_firstline_7.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_7.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_secondline_7.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_secondline_7.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_7.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_7.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_7.setVisibility(visible);
                }
                break;
            case 8:
                if (line == 1){
                    subject_word_firstline_8.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_firstline_8.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_firstline_8.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_8.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_secondline_8.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_secondline_8.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_8.setBackgroundColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_8.setStrokeColor(Color.parseColor(bgcolor));
                    subject_word_thirdline_8.setVisibility(visible);
                }
                break;
        }
    }

    private void setText(String text, int line, int index, int visible){
        switch (index){
            case 1:
                if (line == 1){
                    subject_word_firstline_1.setText(text);
                    subject_word_firstline_1.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_1.setText(text);
                    subject_word_secondline_1.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_1.setText(text);
                    subject_word_thirdline_1.setVisibility(visible);
                }
                break;
            case 2:
                if (line == 1){
                    subject_word_firstline_2.setText(text);
                    subject_word_firstline_2.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_2.setText(text);
                    subject_word_secondline_2.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_2.setText(text);
                    subject_word_thirdline_2.setVisibility(visible);
                }
                break;
            case 3:
                if (line == 1){
                    subject_word_firstline_3.setText(text);
                    subject_word_firstline_3.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_3.setText(text);
                    subject_word_secondline_3.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_3.setText(text);
                    subject_word_thirdline_3.setVisibility(visible);
                }
                break;
            case 4:
                if (line == 1){
                    subject_word_firstline_4.setText(text);
                    subject_word_firstline_4.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_4.setText(text);
                    subject_word_secondline_4.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_4.setText(text);
                    subject_word_thirdline_4.setVisibility(visible);
                }
                break;
            case 5:
                if (line == 1){
                    subject_word_firstline_5.setText(text);
                    subject_word_firstline_5.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_5.setText(text);
                    subject_word_secondline_5.setVisibility(visible);
                }else if (line == 3) {
                    subject_word_thirdline_5.setText(text);
                    subject_word_thirdline_5.setVisibility(visible);
                }

                break;
            case 6:
                if (line == 1){
                    subject_word_firstline_6.setText(text);
                    subject_word_firstline_6.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_6.setText(text);
                    subject_word_secondline_6.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_6.setText(text);
                    subject_word_thirdline_6.setVisibility(visible);
                }
                break;
            case 7:
                if (line == 1){
                    subject_word_firstline_7.setText(text);
                    subject_word_firstline_7.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_7.setText(text);
                    subject_word_secondline_7.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_7.setText(text);
                    subject_word_thirdline_7.setVisibility(visible);
                }
                break;
            case 8:
                if (line == 1){
                    subject_word_firstline_8.setText(text);
                    subject_word_firstline_8.setVisibility(visible);
                }else if (line == 2){
                    subject_word_secondline_8.setText(text);
                    subject_word_secondline_8.setVisibility(visible);
                }else if (line == 3){
                    subject_word_thirdline_8.setText(text);
                    subject_word_thirdline_8.setVisibility(visible);
                }
                break;
        }
    }

    private void setAnswerText(String text, int index){
        switch (index){
            case 0:
                subject_word_answer_first.setText(text);
                break;
            case 1:
                subject_word_answer_second.setText(text);
                break;
            case 2:
                subject_word_answer_third.setText(text);
                break;
            case 3:
                subject_word_answer_fourth.setText(text);
                break;
            case 4:
                subject_word_answer_fifth.setText(text);
                break;
            case 5:
                subject_word_answer_sixth.setText(text);
                break;
            case 6:
                subject_word_answer_seventh.setText(text);
                break;
        }
    }

    private void share(View v){
        /*
        menuWindow = new PublishSelectPicPopupWindow(SubjectWordActivity.this,itemsOnClick,list);
        // 显示窗口
        menuWindow.showAtLocation(v,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        */
        ActionSheetDialog actionSheetDialog = new ActionSheetDialog(SubjectWordActivity.this)
                .builder()
                .setTitle("请选择")
                .setCancelable(false)
                .setCanceledOnTouchOutside(true);
        for (String s : list){
            actionSheetDialog.addSheetItem(s, null, new ActionSheetDialog.OnSheetItemClickListener() {
                @Override
                public void onClick(int which) {
                    if (which == 1){
                        //分享给微信好友
                        String des = "分享自微句";
                        if (wordinfoModel != null){
                            des = wordinfoModel.getTitle();
                        }
                        String content = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                        shareToWX(1,url,content,des);
                    }else if (which == 2){
                        //分享到朋友圈
                        String title = "【"+homeDataModel.getName()+"】";
                        if (wordinfoModel != null){
                            title += wordinfoModel.getTitle();
                        }
                        String c = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                        shareToWX(0,url,c,title);
                    }else if (which == 3){
                        //分享到qq
                        String title = "【"+homeDataModel.getName()+"】";
                        if (wordinfoModel != null){
                            title += wordinfoModel.getTitle();
                        }
                        String c = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                        shareToQQ(0,url,c,title);
                    }else if (which == 4){
                        //qq空间
                        String title = "【"+homeDataModel.getName()+"】";
                        if (wordinfoModel != null){
                            title += wordinfoModel.getTitle();
                        }
                        String c = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                        shareToQQ(1,url,c,title);
                    }
                }
            });
        }
        actionSheetDialog.show();
    }

    //分享到qq
    private Bundle params;
    private void shareToQQ(int type, String url,String title,String content){
        params = new Bundle();
        if (type == 0){//分享给QQ好友
            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
            params.putString(QQShare.SHARE_TO_QQ_TITLE, title);// 标题
            params.putString(QQShare.SHARE_TO_QQ_SUMMARY, content);// 摘要
            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,url);// 内容地址
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"http://image.daydayrise.net/360@2x.png");// 网络图片地址
            params.putString(QQShare.SHARE_TO_QQ_EXT_INT, "");//其它附加功能
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    CustomApplication.mTencent.shareToQQ(SubjectWordActivity.this, params, mIUiListener);
                }
            });
        }else {
            //分享到QQ空间
            params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
            params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);// 标题
            params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, content);// 摘要
            params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL,url);// 内容地址
            ArrayList<String> imgUrlList = new ArrayList<>();
            imgUrlList.add("http://image.daydayrise.net/360@2x.png");
            params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imgUrlList);// 图片地址
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    CustomApplication.mTencent.shareToQzone(SubjectWordActivity.this, params, mIUiListener);
                }
            });
        }

    }

    // 为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.first_button://分享给微信好友
                    String des = "分享自微句";
                    if (wordinfoModel != null){
                        des = wordinfoModel.getTitle();
                    }
                    String content = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                    shareToWX(1,url,content,des);
                    break;
                case R.id.second_button://分享到微信朋友圈
                    String title = "【"+homeDataModel.getName()+"】";
                    if (wordinfoModel != null){
                        title += wordinfoModel.getTitle();
                    }
                    String c = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                    shareToWX(0,url,title,c);
                    break;
                case R.id.third_button:
                    break;
                default:
                    break;
            }

        }
    };

    //分享到微信
    private void shareToWX(int type,String url,String title,String content){
        startTime();
        //初始化一个WXWebpageObject对象，填写url
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        //用WXWebpageObject对象初始化一个WXMediaMessage对象，填写标题、描述
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = content;
        Bitmap thumb = BitmapFactory.decodeResource(getResources(),R.mipmap.icon);
        msg.thumbData = CommonUtils.bitmapToByteArray(thumb,true);
        //构成一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = msg;
        req.scene = type==0 ? SendMessageToWX.Req.WXSceneTimeline:SendMessageToWX.Req.WXSceneSession;
        CustomApplication.mWxApi.sendReq(req);

    }

    private void getShareGold(){
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        String url = "miniapp/"+wordUserinfoModel.getAppid()+"/usershare.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                final CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    wordUserinfoModel = GsonTools.getWordUserinfoModel(data);
                    new Handler().postDelayed(new Runnable(){
                        public void run() {
                            ToastUtil.show(SubjectWordActivity.this,commonModel.getMsg());
                            subject_word_gold.setText(""+wordUserinfoModel.getGoldcount());
                            startTime();
                        }
                    }, 10);
                }
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    /**
     * 广播接收者
     * @author Gordon
     */
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BroadcastDefine.WX_Share_Result)) {
                //微信分享成功
                getShareGold();
            }
        }
    };

    /**
     * 注册广播
     * @author Gordon
     */
    public void registerBoradcastReceiver(){
        IntentFilter myIntentFilter = new IntentFilter();
        //添加接收广播类型
        myIntentFilter.addAction(BroadcastDefine.WX_Share_Result);
        //注册广播
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    private void signDo(){
        if (wordUserinfoModel == null){
            return;
        }
        avi.show();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
        String url = "miniapp/"+wordUserinfoModel.getAppid()+"/usersign.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
            @Override
            public void onSuccess(String data) {
                avi.hide();
                Log.e("TAG",data);
                final CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    wordUserinfoModel = GsonTools.getWordUserinfoModel(data);
                    new Handler().postDelayed(new Runnable(){
                        public void run() {
                            ToastUtil.show(SubjectWordActivity.this,commonModel.getMsg());
                            subject_word_gold.setText(""+wordUserinfoModel.getGoldcount());
                        }
                    }, 10);
                }else {
                    ToastUtil.show(SubjectWordActivity.this,"你已领取");
                }
            }
            @Override
            public void onError(String msg) {
                avi.hide();
            }
        });
    }

    /**
     * 第一种方法 使用android封装好的 CountDownTimer
     * 创建一个类继承 CountDownTimer
     */
    class CountDownTime extends CountDownTimer {

        //构造函数  第一个参数代表总的计时时长  第二个参数代表计时间隔  单位都是毫秒
        public CountDownTime(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) { //每计时一次回调一次该方法
            subject_word_level.setText("倒计时:"+l/1000+"秒");
        }

        @Override
        public void onFinish() { //计时结束回调该方法
            subject_word_level.setText("回答超时");
            isTimeOut = true;
            checkTimeOut();
        }
    }

    private void startTime(){
        if (historySubjectModel != null){
            return;
        }
        if (wordUserinfoModel == null){
            return;
        }
        if (mTime != null){
            mTime.cancel();
            mTime = null;
        }
        isTimeOut = false;
        mTime = new SubjectWordActivity.CountDownTime(wordinfoModel.getTimeout()*1000, 1000);//初始化对象
        mTime.start();
    }

    private void stopTime(){
        if (historySubjectModel != null){
            return;
        }
        if (wordUserinfoModel == null){
            return;
        }
        if (mTime != null){
            mTime.cancel();
            mTime = null;
        }
    }

    private void checkTimeOut(){
        if (isTimeOut == true && isChecking == false){
            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
            map.put(APIKey.idKey,wordinfoModel.getId()+"");
            String url = "miniapp/"+wordUserinfoModel.getAppid()+"/replytimeout.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
                @Override
                public void onSuccess(String data) {
                    Log.e("TAG","提示："+data+"\n");
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        ToastUtil.show(SubjectWordActivity.this,commonModel.getMsg());
                        startTime();
                        GameTimeOutModel model = GsonTools.getGameTimeOutModel(data);
                        wordUserinfoModel.setGoldcount(model.getGold());
                        subject_word_gold.setText(""+wordUserinfoModel.getGoldcount());
                        if (wordUserinfoModel.getGoldcount() < model.getRemovegold()){
                            back();
                        }
                    }else {
                        ToastUtil.show(SubjectWordActivity.this,commonModel.getMsg());
                        back();
                    }
                }
                @Override
                public void onError(String msg) {

                }
            });
        }
    }

    private void back(){
        stopTime();
        finish();
    }

    //监听返回键(有软键盘的情况下想直接返回，需要拦截KeyEvent.ACTION_UP事件)
    @Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
        {
            Log.e("TAG","点击了返回\n");
            back();
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopTime();
    }

    @Override
    protected void onResume() {
        super.onResume();
        startTime();
    }

}

